package com.ocm.tourism.core.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ocm.tourism.core.dao.BusinessDao;
import com.ocm.tourism.core.dao.FileInfoDao;
import com.ocm.tourism.core.domain.Album;
import com.ocm.tourism.core.domain.FileInfo;
import com.ocm.tourism.core.domain.RepositoryType;
import com.ocm.tourism.core.model.ImageItem;
import com.ocm.tourism.picasaweb.service.ImageUploaderService;
import com.ocm.tourism.webclient.service.WebClientManager;

@Service
public class FileInfoServiceImpl implements FileInfoService {

   @Autowired
   FileInfoDao fileInfoDao;

   @Autowired
   ImageUploaderService imageUploaderService;

   @Autowired
   BusinessDao businessDao;

   @Value("${album_tayninh_id}")
   private String mainAlbumId; 

   @Override
   public FileInfo find(Long id) {
      FileInfo fileInfo = fileInfoDao.find(id);
      return fileInfo;
   }

   @Override
   @Transactional(readOnly=false, propagation=Propagation.REQUIRED)
   public Album createAlbum(Long businessId) {
      String albumName = createAlbumName(businessId);
      String content_location = imageUploaderService.createAlbum(albumName);
      Album album = new Album();
      album.setAlbumName(albumName);
      album.setExternalUrl(content_location);
      album.setRepositoryType(RepositoryType.PICASA);
      album.setBusinessId(businessId);
      return album;
   }

   @Override
   @Transactional(readOnly=false, propagation=Propagation.REQUIRED)
   public void uploadFile(Long businessId, ImageItem imageItem) {
      Album album = this.businessDao.findByBusiness(businessId);
      if (album == null){
         album = createAlbum(businessId);
      }
      businessDao.saveAlbum(album);
      this.imageUploaderService.upload(album.getExternalUrl(), imageItem);
   }

   @Override
   @Transactional(readOnly=false, propagation=Propagation.REQUIRED)
   public void deleteFile(Long businessId, String fileId) {
      Album album = this.businessDao.findByBusiness(businessId);
      this.imageUploaderService.deleteFile(album.getExternalUrl(), fileId);
      businessDao.saveAlbum(album);
   }
   
   private String createAlbumName(long businessId){
      String albumName = "business_"+businessId;
      return albumName;
   }

   @Override
   public List<ImageItem> retrieveFiles(Long businessId) {
      List<ImageItem> imageItems = null;
      Album album = this.businessDao.findByBusiness(businessId);
      if (album != null){
         imageItems = this.imageUploaderService.retriveFiles(album.getExternalUrl());
      }
      return imageItems;
   }

   @Override
   public List<ImageItem> retrieveMainPicture() {
      List<ImageItem> imageItems = WebClientManager.instance().getMainImageItems(this.imageUploaderService, this.mainAlbumId);
      return imageItems;
   }
}
